package com.shycloud.mido.music.controller;

import cn.hutool.core.map.MapUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.music.domain.MisMusicBanner;
import com.shycloud.mido.music.domain.MisMusicCategory;
import com.shycloud.mido.music.mapper.MisMusicBannerMapper;
import com.shycloud.mido.music.service.MisMusicBannerService;
import com.shycloud.mido.music.service.MisMusicCategoryService;
import com.shycloud.mido.music.service.MisMusicRecommendService;
import com.shycloud.mido.music.vo.RecommendConfirmEditVO;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;

/**
 * 首页推荐模块controller
 *
 * @author nianhua.jiang
 * @date 2020/8/31 15:57
 **/
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/recommend")
@ApiOperation(value = "首页推荐模块controller")
@ApiIgnore
public class MusicRecommendController {

	/** 首页轮播图 service */
	private MisMusicBannerService bannerService;
	/** 曲集/曲目 推荐模块 service */
	private MisMusicRecommendService recommendService;
	/** 类别 */
	private MisMusicCategoryService categoryService;
	/** 首页轮播图 mapper */
	private MisMusicBannerMapper misMusicBannerMapper;

	@GetMapping("getRecommend")
	public R getRecommend(Integer cateId, String city) {

		Map<String, Object> resultMap = MapUtil.newHashMap();

		//自定义标题
		MisMusicCategory category = categoryService.getById(cateId);

		String specialStatus = category.getSpecialStatus();

		//推荐曲集
		if ("1".equals(category.getIsSpecial())) {

			resultMap.put("banner", null);
			resultMap.put("recommendCollection", null);
			resultMap.put("recommendMusic", null);

			Map<String, String> titleMap = MapUtil.newHashMap();

			if (specialStatus.startsWith("1")) {

				//根据城市查询出代理商
				List<Integer> optIds = misMusicBannerMapper.getOptIdByCity(city);

				List<MisMusicBanner> resList;

				if (optIds != null && optIds.size() > 0) {
					resList = misMusicBannerMapper.selectList(
							new QueryWrapper<MisMusicBanner>()
									.eq("category_id", cateId)
									.eq("status", 0)
									.and(x -> x.eq("opt_id", optIds.get(0))
											.or()
											.eq("opt_id", 999999))
									.orderByAsc(MisMusicBanner.COL_SORT));
				} else {
					//没有该城市运营商,全部显示总部 banner
					resList = misMusicBannerMapper.selectList(
							new QueryWrapper<MisMusicBanner>()
									.eq("category_id", cateId)
									.eq("status", 0)
									.eq("opt_id", 999999)
									.orderByAsc(MisMusicBanner.COL_SORT));
				}
				resultMap.put("banner", resList);
			}

			if (specialStatus.startsWith("1", 1)) {
				resultMap.put("recommendCollection",
						recommendService.getRecommendCollection(cateId));
				resultMap.put("recommendCollectionCount",
						recommendService.getRecommendCollectionCount(cateId));
				titleMap.put("collectionTitle",
						StringUtils.isBlank(category.getCollectionTitle()) ? "推荐曲集"
								: category.getCollectionTitle());
			}

			if (specialStatus.startsWith("1", 2)) {
				resultMap.put("recommendMusic", recommendService.getRecommendMusic(cateId));
				resultMap.put("recommendMusicCount",
						recommendService.getRecommendMusicCount(cateId));
				titleMap.put("musicTitle", StringUtils.isBlank(category.getMusicTitle()) ? "推荐曲目"
						: category.getMusicTitle());
			}
			resultMap.put("title", titleMap);
		}

		return new R(resultMap);
	}

	/**
	 * 后台获取Banner
	 *
	 * @param cateId
	 * @return
	 */
	@GetMapping("getRecommendAdmin")
	public R getRecommendAdmin(Integer cateId) {

		Map<String, Object> resultMap = MapUtil.newHashMap();

		//轮播图
		resultMap.put("banner", bannerService.list(new QueryWrapper<MisMusicBanner>()
				.eq(MisMusicBanner.COL_STATUS, "0").and(x -> x.eq("category_id", cateId))
				.orderByAsc(MisMusicBanner.COL_SORT)));

		//推荐曲目
		resultMap.put("recommendCollection", recommendService.getRecommendCollectionAdmin(cateId));

		//推荐曲集
		resultMap.put("recommendMusic", recommendService.getRecommendMusicAdmin(cateId));

		MisMusicCategory cateEntity = categoryService.getBaseMapper().selectOne(
				new QueryWrapper<MisMusicCategory>().eq("id", cateId));

		resultMap.put("musicTilte", cateEntity.getMusicTitle());

		resultMap.put("collectionTitle", cateEntity.getCollectionTitle());

		return new R(resultMap);
	}

	/**
	 * 获取更多曲集
	 *
	 * @param current
	 * @param size
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/9/1 9:18
	 **/
	@GetMapping("getMoreThenCollection")
	public R getMoreThenCollection(Integer current, Integer size, Integer cateId) {
		return new R(recommendService.getMoreThenCollection(new Page(current, size), cateId));
	}

	/**
	 * 获取更多曲目
	 *
	 * @param current
	 * @param size
	 * @return
	 * @author nianhua.jiang
	 * @date 2020/9/1 9:18
	 **/
	@GetMapping("getMoreThenMusic")
	public R getMoreThenMusic(Integer current, Integer size, Integer cateId) {
		return new R(recommendService.getMoreThenMusic(new Page(current, size), cateId));
	}

	/**
	 * 新增类别编辑确定接口
	 *
	 * @return
	 */
	@PostMapping("/confirmEdit")
	@ApiOperation("确定新增内容接口")
	public R confirmEdit(@RequestBody RecommendConfirmEditVO confirmEditVO) {
		Boolean result = false;
		try {
			result = recommendService.confirmEdit(confirmEditVO);
		} catch (BusinessException e) {
			return new R<>(CommonConstants.FAIL, e.getMessage(), false);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			return new R<>(CommonConstants.FAIL, "内部错误，编辑失败，请联系管理员", false);
		}
		return new R<>(result);
	}

}
